package com.ie.controller.admin;

import com.ie.common.http.ResponseResult;
import com.ie.common.security.anno.AuthIgrone;
import com.ie.entity.admin.GoodsEntity;
import com.ie.service.admin.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.baomidou.mybatisplus.extension.toolkit.Db.getById;

@Slf4j
@RestController
@RequestMapping("/api/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    private static String fileName;
    private static byte[] filecontent;
    @AuthIgrone
    @PostMapping("/fileInit")
    public void fileInit(@RequestBody MultipartFile file) {
        // MultipartFile对象不能在另一个方法中使用，所以把文件对象变成字节数组
        try {
            filecontent = file.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fileName = file.getOriginalFilename();
        //log.info("文件名:{}",fileName);
    }
    @AuthIgrone //TODO忽略权限
    @PostMapping("/add")
    public ResponseResult<Map<String,String>> addGoods(@RequestBody GoodsEntity goodsEntity){
        goodsEntity.setFileName(fileName);
        goodsEntity.setLogoFile(filecontent);
        filecontent = new byte[0];
        return goodsService.addGoods(goodsEntity);
    }
    @AuthIgrone
    @PostMapping("/getGoods")
    public ResponseResult<Map<String,Object>> getGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoods(goodsEntity);
    }
    //广告商品的获取
    @AuthIgrone
    @PostMapping("/getAdvGoods")
    public ResponseResult<List<GoodsEntity>> getAdvGoods(){
        return goodsService.getAdvGoods();
    }

    @AuthIgrone  //TODO忽略权限
    @PostMapping("/getGoodsById")
    public ResponseResult<GoodsEntity> getGoodsById(@RequestBody GoodsEntity goodsEntity) {
        // 调用服务层方法获取商品详情
        return goodsService.getGoodsById(goodsEntity);
    }
    @AuthIgrone  //TODO忽略权限
    @PostMapping("/getGoodsIndex")
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoodsIndex(goodsEntity);
    }
    @AuthIgrone //TODO忽略权限
    @PostMapping("/getGoodsByType")
    public ResponseResult<List<GoodsEntity>> getGoodsByType(@RequestBody GoodsEntity goodsEntity){
        return null;
    }

    @AuthIgrone //TODO忽略权限
    @PostMapping("/delete")
    public ResponseResult<Map<String,String>> deleteGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.deleteGoods(goodsEntity);
    }
}
